﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using FileHelpers;
using FileHelpers.DataLink;
using Import.FileDefinitions;

namespace Import
{
    class Program
    {
        static SociologicalDataDataContext _db;

        static void Main(string[] args)
        {
            _db = new SociologicalDataDataContext();
            ImportFederalElectionResultsQld2004(@"C:\Documents and Settings\n7147406\My Documents\My Dropbox\GenerateKmlProject\Test Files\Federal Election Results-Qld-2004.xls");
            ImportQldFederalResults2PartyPreferredByElectorate2004(@"C:\Documents and Settings\n7147406\My Documents\My Dropbox\GenerateKmlProject\Test Files\Qld_Federal Results 2 Party Preferred by Electorate-2004.csv");
            QldFederalResultsByElectorate2004(@"C:\Documents and Settings\n7147406\My Documents\My Dropbox\GenerateKmlProject\Test Files\Qld_FederalResults by Electorate-2004.xls");
            ImportQldFederalStateElectorateMapping(@"C:\Documents and Settings\n7147406\My Documents\My Dropbox\GenerateKmlProject\Test Files\Qld_Federal-State Electorate Mapping.xls");
            ImportQldStateResultsByElectorate2006(@"C:\Documents and Settings\n7147406\My Documents\My Dropbox\GenerateKmlProject\Test Files\Qld_State Results by Electorate-2006.xls");
            _db.Dispose();
        }

        static void ImportFederalElectionResultsQld2004(string filePath)
        {
            CheckFile(filePath);
            var provider = new ExcelStorage(typeof(FederalElectionResultsQld2004FileDefinition), filePath, 2, 1);
            var results = provider.ExtractRecords() as FederalElectionResultsQld2004FileDefinition[];
            _db.TruncateFederalElectionResultsQld2004();
            foreach (var item in results)
            {
                var record = new FederalElectionResultsQld2004();
                record.HeldSince = item.HeldSince;
                record.MP = item.MP;
                record.Party = item.Party;
                record.Percentage = item.Percentage;
                record.PreviouslyHeld = item.PreviouslyHeld;
                _db.FederalElectionResultsQld2004s.InsertOnSubmit(record);
            }
            _db.SubmitChanges();
        }

        static void ImportQldFederalResults2PartyPreferredByElectorate2004(string filePath)
        {
            CheckFile(filePath);
            var engine = new FileHelperEngine(typeof(QldFederalResults2PartyPreferredByElectorate2004FileDefinition));
            var results = engine.ReadFile(filePath) as QldFederalResults2PartyPreferredByElectorate2004FileDefinition[];
            _db.TruncateQldFederalResults2PartyPreferredByElectorate2004();
            foreach (var item in results)
            {
                var record = new QldFederalResults2PartyPreferredByElectorate2004();
                record.AustralianLaborPartyPercentage = item.AustralianLaborPartyPercentage;
                record.AustralianLaborPartyVotes = item.AustralianLaborPartyVotes;
                record.DivisionID = item.DivisionID;
                record.DivisionNm = item.DivisionNm;
                record.ID = record.ID;
                record.LiberalNationalCoalitionPercentage = item.LiberalNationalCoalitionPercentage;
                record.LiberalNationalCoalitionVotes = item.LiberalNationalCoalitionVotes;
                record.PartyAb = item.PartyAb;
                record.StateAb = item.StateAb;
                record.Swing = item.Swing;
                record.TotalVotes = item.TotalVotes;
                _db.QldFederalResults2PartyPreferredByElectorate2004s.InsertOnSubmit(record);
            }
            _db.SubmitChanges();
        }

        static void QldFederalResultsByElectorate2004(string filePath)
        {
            CheckFile(filePath);
            var provider = new ExcelStorage(typeof(QldFederalResultsByElectorate2004FileDefinition), filePath, 3, 1);
            var results = provider.ExtractRecords() as QldFederalResultsByElectorate2004FileDefinition[];
            _db.TruncateQldFederalResults2PartyPreferredByElectorate2004();
            foreach (var item in results)
            {
                if (item.Division == "National Total") continue;
                var record = new QldFederalResultsByElectorate2004();
                record.ALP = item.ALP;
                record.DEM = item.DEM;
                record.Division = item.Division;
                record.GRN = item.GRN;
                record.LP = item.LP;
                record.NP = item.NP;
                record.OTH = item.OTH;
                record.State = item.State;
                record.TPPALP = item.TPPALP;
                record.TPPLP = item.TPPLP;
                _db.QldFederalResultsByElectorate2004s.InsertOnSubmit(record);
            }
            _db.SubmitChanges();
        }

        static void ImportQldFederalStateElectorateMapping(string filePath)
        {
            CheckFile(filePath);
            var provider = new ExcelStorage(typeof(QldFederalStateElectorateMappingFileDefinition), filePath, 2, 1);
            var results = provider.ExtractRecords() as QldFederalStateElectorateMappingFileDefinition[];
            _db.TruncateQldFederalStateElectorateMapping();
            foreach (var item in results)
            {
                var record = new QldFederalStateElectorateMapping();
                record.FederalSeat = item.FederalSeat;
                record.StateSeat = item.StateSeat;
                _db.QldFederalStateElectorateMappings.InsertOnSubmit(record);
            }
            _db.SubmitChanges();
        }

        static void ImportQldStateResultsByElectorate2006(string filePath)
        {
            CheckFile(filePath);
            var provider = new ExcelStorage(typeof(QldStateResultsByElectorate2006FileDefinition), filePath, 4, 1);
            var results = provider.ExtractRecords() as QldStateResultsByElectorate2006FileDefinition[];
            _db.TruncateQldStateResultsByElectorate2006();
            foreach (var item in results)
            {
                if (item.BoothsIn == "Incorrect Candidate Selected.") continue;
                var record = new QldStateResultsByElectorate2006();
                record.BoothsIn = item.BoothsIn;
                record.Candidate1 = item.Candidate1;
                record.Candidate2 = item.Candidate2;
                record.DistrictName = item.DistrictName;
                record.No = item.No;
                record.Party1 = item.Party1;
                record.Party2 = item.Party2;
                record.Percentage1 = item.Percentage1;
                record.Percentage2 = item.Percentage2;
                record.Votes1 = item.Votes1;
                record.Votes2 = item.Votes2;
                _db.QldStateResultsByElectorate2006s.InsertOnSubmit(record);
            }
            _db.SubmitChanges();
        }

        static void CheckFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("Failed to import Federal Election Results Qld 2004.", filePath);
            }
        }
    }
}
